package system.controller;

import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import common.controller.base.BaseController;
import common.model.po.SysBusinessCode;
import common.model.request.base.PaginationRequest;
import common.model.response.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import system.model.request.DeleteBusinessCodeRequest;
import system.model.request.SaveBusinessCodeRequest;
import system.model.request.SearchBusinessCodeRequest;
import system.model.request.UpdateBusinessCodeValidRequest;
import system.service.SysBusinessCodeService;

import javax.annotation.Resource;
import java.util.List;
import java.util.TreeMap;

/**
 * 业务编码管理
 *
 * @author 米泽鹏
 * @since 2022-11-29
 */
@Api(tags = "业务编码管理")
@RestController
@RequestMapping("/system/sys-business-code")
public class SysBusinessCodeController extends BaseController {

	@Resource
	private SysBusinessCodeService businessCodeService;

	@ApiOperation(value = "获取业务编码数据", response = Result.class)
	@GetMapping("/readOnly/getAllBusinessCodeFromCache")
	public Result<TreeMap<String, List<Tree<Long>>>> getAllBusinessCodeFromCache() {
		TreeMap<String, List<Tree<Long>>> businessCodes = businessCodeService.getAllBusinessCode();
		return successWithData(businessCodes);
	}

	@PostMapping("/readOnly/pageBusinessCode")
	@ApiOperation(value = "业务编码分页查询", notes = "业务编码分页查询", response = Result.class)
	public Result<Page<SysBusinessCode>> pageBusinessCode(@ApiParam(value = "业务编码分页查询请求", required = true) @RequestBody @Validated PaginationRequest<SearchBusinessCodeRequest, SysBusinessCode> request) {
		return successWithData(businessCodeService.pageBusinessCode(request));
	}

	@PostMapping("/saveBusinessCode")
	@ApiOperation(value = "保存业务编码", notes = "保存业务编码", response = Result.class)
	public Result<?> saveBusinessCode(@ApiParam(value = "保存业务编码请求", required = true) @RequestBody @Validated SaveBusinessCodeRequest request) {
		businessCodeService.saveBusinessCode(request);
		return success();
	}

	@DeleteMapping("/deleteBusinessCode")
	@ApiOperation(value = "删除业务编码", notes = "删除业务编码", response = Result.class)
	public Result<?> deleteBusinessCode(@ApiParam(value = "删除业务编码请求", required = true) @RequestBody @Validated DeleteBusinessCodeRequest request) {
		businessCodeService.deleteBusinessCode(request);
		return success();
	}

	@GetMapping("/readOnly/getAllBusinessCodeById/{businessCodeId}")
	@ApiOperation(value = "根据id查询业务编码", notes = "根据id查询业务编码", response = Result.class)
	public Result<List<Tree<Long>>> getAllBusinessCodeById(@ApiParam(value = "业务编码", required = true) @PathVariable Long businessCodeId) {
		return successWithData(businessCodeService.getAllBusinessCodeById(businessCodeId));
	}

	@PutMapping("/updateBusinessCodeValid")
	@ApiOperation(value = "更新业务编码启用状态", notes = "更新业务编码启用状态", response = Result.class)
	public Result<Boolean> updateBusinessCodeValid(@ApiParam(value = "更新业务编码启用状态请求", required = true) @RequestBody @Validated UpdateBusinessCodeValidRequest request) {
		return successWithData(businessCodeService.updateBusinessCodeValid(request));
	}

}
